package com.yang.srb.sms.controller;


import com.yang.common.exception.Assert;
import com.yang.common.exception.BusinessException;
import com.yang.common.result.R;
import com.yang.common.result.ResponseEnum;
import com.yang.common.util.RegexValidateUtils;
import com.yang.srb.sms.client.CoreUserInfoClient;
import com.yang.srb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")

@Slf4j
public class ApiSmsController {

    @Resource
    private SmsService smsService;

    @Resource
    private CoreUserInfoClient coreUserInfoClient;

    @ApiOperation("获取验证码")
    @GetMapping("/send/{mobile}")
    public R send(
            @ApiParam(value = "手机号", required = true)
            @PathVariable String mobile
    ) {

        //判断手机号不能为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //是否是合法手机号码
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);

        //手机号是否注册
        boolean result = coreUserInfoClient.checkMobile(mobile);
      System.out.println("result = " + result);
      Assert.isTrue(result == false, ResponseEnum.MOBILE_EXIST_ERROR);


        smsService.send(mobile);
        return R.ok().message("短信发送成功");

    }
}
